package com.pianzhu.blockchainemr.service.impl;

import com.pianzhu.blockchainemr.mapper.PatientMapper;
import com.pianzhu.blockchainemr.pojo.MedicalRecords;
import com.pianzhu.blockchainemr.pojo.result.MedicalRecordListResult;
import com.pianzhu.blockchainemr.pojo.Patient;
import com.pianzhu.blockchainemr.pojo.result.RegistryRecordListResult;
import com.pianzhu.blockchainemr.service.PatientService;
import com.pianzhu.blockchainemr.utils.CurrentHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class PatientServiceImpl implements PatientService {
    @Autowired
    private PatientMapper patientMapper;

    @Override
    public List<MedicalRecordListResult> getMedicalRecord(String username) {
        return patientMapper.getMedicalRecord(username);
    }

    @Override
    public List<RegistryRecordListResult> getRegistryRecord(String username) {

        return patientMapper.getRegistryRecord(username);
    }

    @Override
    @Transactional
    public void register(MedicalRecords medicalRecords) {
        String username = CurrentHolder.getCurrentLocal();
        Patient p = patientMapper.selectPatient(username);
        medicalRecords.setPatientId(p.getId());
        medicalRecords.setRegisterTime(LocalDateTime.now());
        patientMapper.register(medicalRecords);
    }

    @Override
    public List<MedicalRecords> getProfileList(String username) {
        List<MedicalRecords> medicalRecordsList = patientMapper.getMedicalRecordList(username);
        return medicalRecordsList;
    }

    @Override
    public List<String> getSufferList(String username) {
        List<String> sufferList = patientMapper.getDiseasesOnceSuffered(username);
        return sufferList;
    }
}
